/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.testis.gui.mvc.view;

import java.awt.*;

/**
 *
 * @author selcuk
 */
public class WindowLocationProvider {

    protected java.awt.Window m_window;

    public WindowLocationProvider(java.awt.Window window) {
        if (window == null) {
            throw new NullPointerException();
        }
        m_window = window;
    }

    // centers the dialog within the screen [1.1]
    public void centerScreen() {
        Dimension dim = m_window.getToolkit().getScreenSize();
        Rectangle abounds = m_window.getBounds();
        m_window.setLocation((dim.width - abounds.width) / 2,
                (dim.height - abounds.height) / 2);
        m_window.setVisible(true);
        m_window.requestFocus();
    }

    // centers the dialog within the parent container [1.1]
    public void centerParent() {
        int x;
        int y;

        // Find out our parent
        Container myParent = m_window.getParent();
        Point topLeft = myParent.getLocationOnScreen();
        Dimension parentSize = myParent.getSize();

        Dimension mySize = m_window.getSize();

        if (parentSize.width > mySize.width) {
            x = ((parentSize.width - mySize.width) / 2) + topLeft.x;
        } else {
            x = topLeft.x;
        }

        if (parentSize.height > mySize.height) {
            y = ((parentSize.height - mySize.height) / 2) + topLeft.y;
        } else {
            y = topLeft.y;
        }

        m_window.setLocation(x, y);
        m_window.setVisible(true);
        m_window.requestFocus();
    }
}
